package org.balabala.gateway.controller;

import com.alibaba.fastjson.TypeReference;
import feign.FeignException;
import io.lettuce.core.RedisCommandExecutionException;
import lombok.extern.slf4j.Slf4j;
import org.balabala.gateway.entity.BrowseInfo;
import org.balabala.gateway.entity.Comment;
import org.balabala.gateway.entity.User;
import org.balabala.gateway.entity.Video;
import org.balabala.gateway.feign.UserFeign;
import org.balabala.gateway.redis.SessionManager;
import org.balabala.gateway.redis.UserManager;
import org.balabala.gateway.redis.VideoManager;
import org.balabala.gateway.response.Resp;
import org.balabala.gateway.response.UnifiedResponse;
import org.balabala.gateway.response.ResponseInfo;
import org.balabala.gateway.utils.MD5Util;
import org.balabala.gateway.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/api/user")
public class UserRouter {

    //cookie时间设置为一天
    private final static int COOKIE_LIFE_CYCLE = 24 * 60;

    @Resource
    private UserFeign userFeign;
    @Autowired
    private UserManager userManager;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private VideoManager videoManager;

    private void rpcDataUtil(UnifiedResponse response, Resp resp) {
        if (resp.getCode() == 0) {
            response.setCode(ResponseInfo.SUCCEED);
            PageUtils pageInfo = resp.getData(new TypeReference<PageUtils>() {
            });
            response.setData(pageInfo);
        } else {
            response.setCode(ResponseInfo.OPERATE_FAILED);
            response.setErrorMessage((String) resp.get("msg"));
        }
    }


    /**
     * 用户登录
     *
     * @param response
     * @param user
     * @return
     */
    @PostMapping("/login")
    public UnifiedResponse userLogin(HttpServletResponse response, @RequestBody User user) {

        UnifiedResponse unifiedResponse = new UnifiedResponse();
        if (user == null) {
            log.error("userLogin parameter user is null");
            unifiedResponse.setCode(ResponseInfo.OPERATE_FAILED);
            unifiedResponse.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            return unifiedResponse;
        }

        log.info("userLogin parameter user phoneNumber : " + user.getPhoneNumber());

        try {
            Resp resp = userFeign.login(user);
            if (resp.getCode() == 0) {
                unifiedResponse.setCode(ResponseInfo.SUCCEED);
                user = resp.getData(new TypeReference<User>() {
                });
                try {
                    userManager.setUser(user);
                    unifiedResponse.setData(new User[]{user});
                    sessionManager.setSession(user.getUid(), MD5Util.getMd5(user.getUid()));
                } catch (RedisCommandExecutionException e) {
                    log.error("set user and session to redis error : " + e);
                }
                Cookie cookie = new Cookie("uid", String.valueOf(user.getUid()));
                cookie.setMaxAge(COOKIE_LIFE_CYCLE);
                response.addCookie(cookie);
            } else {
                unifiedResponse.setCode(ResponseInfo.OPERATE_FAILED);
                unifiedResponse.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            log.error("userFeign.login request error : " + e);
            unifiedResponse.setCode(ResponseInfo.SERVER_FAILED);
            unifiedResponse.setErrorMessage(ResponseInfo.RPC_ERROR);
            return unifiedResponse;
        }

        return unifiedResponse;
    }


    /**
     * 退出登录
     *
     * @param uid
     * @return
     */
    @PostMapping("/logout")
    public UnifiedResponse userLogout(Long uid) {

        UnifiedResponse response = new UnifiedResponse();
        if (uid == null) {
            log.error("userLogout parameter uid is null");
            response.setCode(ResponseInfo.OPERATE_FAILED);
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            return response;
        } else {
            log.info("userLogout parameter uid : " + uid);
            try {
                userManager.deleteUser(uid);
                sessionManager.deleteSession(uid);
                response.setCode(ResponseInfo.SUCCEED);
            } catch (RedisCommandExecutionException e) {
                response.setCode(ResponseInfo.SERVER_FAILED);
                log.error("delete user and session to redis error : " + e);
            }
        }

        return response;
    }


    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public UnifiedResponse userRegister(@RequestBody User user) {
        UnifiedResponse response = new UnifiedResponse();
        if (user == null) {
            log.error("userRegister parameter user is null");
            response.setCode(ResponseInfo.OPERATE_FAILED);
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            return response;
        }

        log.info("userRegister parameter user info : " + user);
        try {
            Resp resp = userFeign.register(user);
            if (resp.getCode() == 0) {
                log.info("user register succeed");
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage(resp.get("msg").toString());
                log.error("register err : " + resp.get("msg").toString());
            }
        } catch (FeignException e) {
            log.error("userFeign.register request err : " + e);
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            return response;
        }

        return response;
    }


    /**
     * 获取用户信息
     *
     * @param uid
     * @return
     */
    @GetMapping("/info")
    public UnifiedResponse getUserInfo(Long uid) {
        UnifiedResponse response = new UnifiedResponse();

        if (uid == null) {
            log.error("getUserInfo parameter uid is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        log.info("getUserInfo parameter uid : " + uid);

        User user = null;
        try {
            user = userManager.getUser(uid);
        } catch (RedisCommandExecutionException e) {
            log.error("redis getUser error : " + e);
        }

        if (user == null) {
            try {
                Resp resp = userFeign.getUserInfo(uid);
                if (resp.getCode() == 0) {
                    user = resp.getData(new TypeReference<User>() {
                    });
                    try {
                        userManager.setUser(user);
                    } catch (RedisCommandExecutionException e) {
                        log.error("redis setUser error : " + e);
                    }
                    response.setCode(ResponseInfo.SUCCEED);
                    response.setData(new User[]{user});
                    return response;
                } else {
                    response.setCode(ResponseInfo.OPERATE_FAILED);
                    response.setErrorMessage((String) resp.get("msg"));
                }
            } catch (FeignException e) {
                log.error("userFeign.getUserInfo request err : " + e);
                response.setCode(ResponseInfo.SERVER_FAILED);
                response.setErrorMessage(ResponseInfo.RPC_ERROR);
            }
        } else {
            log.info("getUserInfo rom redis,uid : " + uid);
            response.setCode(ResponseInfo.SUCCEED);
            response.setData(new User[]{user});
        }
        return response;
    }


    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/update")
    public UnifiedResponse updateUserInfo(User user) {
        UnifiedResponse response = new UnifiedResponse();

        if (user == null || user.getUid() == null || user.getId() == null) {
            log.error("updateUserInfo parameter user is null");
            response.setCode(ResponseInfo.OPERATE_FAILED);
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            return response;
        }

        log.info("updateUserInfo parameter user : " + user);

        try {
            Resp resp = userFeign.updateUserInfo(user);
            if (resp.getCode() == 0) {
                response.setCode(ResponseInfo.SUCCEED);
                user = resp.getData(new TypeReference<User>() {
                });
                response.setData(new User[]{user});
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            log.error("userFeign.updateUserInfo request error : " + e);
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
        }

        try {
            userManager.setUser(user);
        } catch (RedisCommandExecutionException e) {
            log.error("redis setUser error : " + e);
        }

        return response;
    }


    /**
     * 查询用户列表(支持模糊查询)
     *
     * @param page
     * @param limit
     * @param key
     * @return
     */
    @GetMapping("/search_all")
    public UnifiedResponse searchAllUser(Integer page, Integer limit, String key, Integer state) {
        UnifiedResponse response = new UnifiedResponse();
        if (page == null || limit == null || state == null) {
            page = 0;
            limit = 20;
            state = 0;
        }

        log.info("searchAllUser parameter page : " + page + " limit : " + limit + " key : " + key + " state : " + state);

        try {
            Resp resp = userFeign.getUserList(page, limit, key, state);
            rpcDataUtil(response, resp);
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.getUserList request error : " + e);
        }

        return response;
    }


    /**
     * 添加浏览记录
     *
     * @param browseInfo
     * @return
     */
    @PostMapping("/add_browse")
    public UnifiedResponse addBrowse(BrowseInfo browseInfo) {
        UnifiedResponse response = new UnifiedResponse();
        if (browseInfo == null || browseInfo.getUid() == null || browseInfo.getVideoId() == null) {
            log.error("addBrowse parameter browseInfo is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        log.info("addBrowse parameter browseInfo : " + browseInfo);

        try {
            Resp resp = userFeign.saveBrowseInfo(browseInfo);
            if (resp.getCode() != 0) {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            } else {
                response.setCode(ResponseInfo.SUCCEED);
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.saveBrowseInfo request error : " + e);
        }

        try {
            Video video = videoManager.getVideo(browseInfo.getVideoId());
            userManager.addUserHistory(browseInfo.getUid(), new Video[]{video});
        } catch (RedisCommandExecutionException e) {
            log.error("redis command userManager.addUserHistory error : " + e);
        }
        return response;

    }


    /**
     * 获取用户浏览记录
     *
     * @param uid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/history")
    public UnifiedResponse getUserHistory(Long uid, Integer page, Integer limit) {
        UnifiedResponse response = new UnifiedResponse();
        if (uid == null) {
            log.error("getUserHistory parameter uid is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        if (page == null || limit == null || page == 0) {
            page = 1;
            limit = 10;
        }
        log.info("getUserHistory parameter page : " + page + "  limit : " + limit + " uid: " + uid);

        if (page == 1 && limit == 10 && userManager.containsKey(String.valueOf(uid))) {
            try {
                List<Object> userHistory = userManager.getUserHistory(uid);
                response.setCode(ResponseInfo.SUCCEED);
                response.setData(userHistory.toArray());
                log.info("getUserHistory from cache");
                return response;
            } catch (RedisCommandExecutionException e) {
                log.error("redis command userManager.getUserHistory error : " + e);
            }
        }

        try {
            Resp resp = userFeign.getUserHistory(uid, page, limit);
            if (resp.getCode() != 0) {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            } else {
                response.setCode(ResponseInfo.SUCCEED);
                List<Video> videos = resp.getData(new TypeReference<List<Video>>() {
                });
                response.setData(videos.toArray());
                try {
                    userManager.addUserHistory(uid, videos.toArray());
                } catch (RedisCommandExecutionException e) {
                    log.error("redis command userManager.addUserHistory error : " + e);
                }
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.getUserHistory request err : " + e);
        }
        return response;
    }


    /**
     * 增加喜欢的视频
     *
     * @param uid
     * @param videoId
     * @return
     */
    @PostMapping("/add_favorite")
    public UnifiedResponse setUserFavourite(Long uid, Long videoId) {
        UnifiedResponse response = new UnifiedResponse();
        if (uid == null || videoId == null) {
            log.error("setUserFavourite uid or videoId is null, uid : " + uid + " videoId : " + videoId);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            return response;
        }

        log.info("setUserFavourite uid : " + uid + " videoId : " + videoId);
        try {
            Resp resp = userFeign.setUserFavourite(uid, videoId);
            if (resp.getCode() == 0) {
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.setUserFavourite request err : " + e);
        }

        return response;
    }


    /**
     * 获取用户喜欢的视频
     *
     * @param uid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/get_favorite")
    public UnifiedResponse getUserFavourite(Long uid, Integer page, Integer limit) {
        UnifiedResponse response = new UnifiedResponse();
        if (uid == null) {
            log.error("getUserFavourite parameter uid is null");
            response.setCode(ResponseInfo.OPERATE_FAILED);
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            return response;
        }

        if (page == null || limit == null) {
            page = 1;
            limit = 20;
        }
        log.info("getUserFavourite parameter page: " + page + " limit: " + limit + " uid:" + uid);

        try {
            Resp resp = userFeign.getUserFavourite(uid, page, limit);
            rpcDataUtil(response, resp);
        } catch (FeignException e) {
            log.error("userFeign.getUserFavourite request error: " + e);
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
        }

        return response;
    }


    /**
     * 添加用户评论
     *
     * @param comment
     * @return
     */
    @PostMapping("/add_comment")
    public UnifiedResponse addComment(Comment comment) {
        UnifiedResponse response = new UnifiedResponse();
        if (comment == null || comment.getUid() == null || comment.getVideoId() == null) {
            log.error("addComment parameter comment is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        log.info("addComment parameter comment : " + comment);

        try {
            Resp resp = userFeign.addComment(comment);
            if (resp.getCode() == 0) {
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.addComment request error : " + e);
        }

        response.setCode(ResponseInfo.SUCCEED);
        return response;
    }


    /**
     * 刪除評論
     *
     * @param comment
     * @return
     */
    @PostMapping("/del_comment")
    public UnifiedResponse deleteComment(Comment comment) {
        UnifiedResponse response = new UnifiedResponse();
        if (comment == null || comment.getCommentId() == null) {
            log.error("deleteComment parameter comment is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        log.info("deleteComment parameter comment : " + comment);

        try {
            Resp resp = userFeign.delComment(comment.getCommentId());
            if (resp.getCode() == 0) {
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.delComment request error : " + e);
        }

        return response;
    }


    /**
     * 获取用户的评论
     *
     * @param uid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/get_comment")
    public UnifiedResponse getUserComment(Long uid, Integer page, Integer limit) {
        UnifiedResponse response = new UnifiedResponse();
        if (uid == null) {
            log.error("getUserComment parameter uid is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        if (page == null || limit == null) {
            page = 0;
            limit = 20;
        }

        log.info("getUserComment parameter uid : " + uid + " page : " + page + " limit : " + limit);

        try {
            Resp resp = userFeign.getUserComments(uid, page, limit);
            rpcDataUtil(response, resp);
        } catch (Exception e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.getUserComments request error : " + e);
        }

        response.setCode(ResponseInfo.SUCCEED);
        return response;
    }


    /**
     * 添加关注信息
     *
     * @param uid
     * @param followUid
     * @return
     */
    @PostMapping("/add_follow")
    public UnifiedResponse addFollow(Long uid, Long followUid) {
        UnifiedResponse response = new UnifiedResponse();
        if (uid == null || followUid == null) {
            log.error("addFollow parameter uid,followUid is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        log.info("addFollow parameter uid : " + uid + " followUid : " + followUid);

        try {
            Resp resp = userFeign.addFollowInfo(uid, followUid);
            if (resp.getCode() == 0) {
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.addFollowInfo request error : " + e);
        }

        return response;
    }


    /**
     * 删除关注信息
     *
     * @param uid
     * @param followUid
     * @return
     */
    @PostMapping("/del_follow")
    public UnifiedResponse delFollow(Long uid, Long followUid) {
        UnifiedResponse response = new UnifiedResponse();
        if (uid == null || followUid == null) {
            log.error("addFollow parameter uid,followUid is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        log.info("delFollow parameter uid : " + uid + " followUid : " + followUid);

        try {
            Resp resp = userFeign.delFollowInfo(uid, followUid);
            if (resp.getCode() == 0) {
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.delFollowInfo request error : " + e);
        }

        return response;
    }


    /**
     * 获取粉丝列表
     *
     * @param uid
     * @return
     */
    @GetMapping("/get_fans")
    public UnifiedResponse getFans(Long uid, Integer page, Integer limit) {
        UnifiedResponse response = new UnifiedResponse();
        if (uid == null) {
            log.error("getFans parameter uid is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        if (page == null || limit == null) {
            page = 0;
            limit = 20;
        }

        log.info("getFans parameter uid : " + uid + " page : " + page + " limit : " + limit);

        try {
            Resp resp = userFeign.getFans(uid, page, limit);
            if (resp.getCode() == 0) {
                PageUtils pageInfo = resp.getData(new TypeReference<PageUtils>() {
                });
                response.setData(pageInfo.getList().toArray());
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.getFans request error : " + e);
        }

        return response;
    }


    /**
     * 获取用户关注列表
     *
     * @param uid
     * @return
     */
    @GetMapping("/get_follow")
    public UnifiedResponse getFollows(Long uid, Integer page, Integer limit) {
        UnifiedResponse response = new UnifiedResponse();
        if (uid == null) {
            log.error("getFollows parameter uid is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        if (page == null || limit == null) {
            page = 0;
            limit = 20;
        }

        log.info("getFollows parameter uid : " + uid + " page : " + page + " limit : " + limit);

        try {
            Resp resp = userFeign.getFollows(uid, page, limit);
            rpcDataUtil(response, resp);
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.getFollows request error : " + e);
        }

        return response;
    }


    /**
     * 批量封禁用户
     *
     * @param map
     * @return
     */
    @PostMapping("/del_users")
    public UnifiedResponse del_users(@RequestBody HashMap<String, Object> map) {
        UnifiedResponse response = new UnifiedResponse();

        if (map == null) {
            log.error("del_users parameter Ids is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        log.info("del_users parameter Ids : " + map);


        try {
            Resp resp = userFeign.del_users(map);
            if (resp.getCode() == 0) {
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.del_users request error : " + e);
        }

        return response;
    }


    /**
     * 批量删除评论
     *
     * @param commentIds
     * @return
     */
    @PostMapping("/batch_del_comment")
    public UnifiedResponse delComment(@RequestBody Long[] commentIds) {
        UnifiedResponse response = new UnifiedResponse();

        if (commentIds != null && commentIds.length == 0) {
            log.error("del_comment parameter Ids is null");
            response.setErrorMessage(ResponseInfo.PARAMETER_ERROR);
            response.setCode(ResponseInfo.OPERATE_FAILED);
            return response;
        }

        log.info("del_comment parameter Ids : " + commentIds);

        try {
            Resp resp = userFeign.deleteComment(commentIds);
            if (resp.getCode() == 0) {
                response.setCode(ResponseInfo.SUCCEED);
            } else {
                response.setCode(ResponseInfo.OPERATE_FAILED);
                response.setErrorMessage((String) resp.get("msg"));
            }
        } catch (
                FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.del_users request error : " + e);
        }

        return response;
    }


    /**
     * 批量查询评论（评论内容模糊查询）
     *
     * @param page
     * @param limit
     * @param key
     * @return
     */
    @GetMapping("/batch_get_comment")
    public UnifiedResponse batchGetComment(Integer page, Integer limit, String key) {
        UnifiedResponse response = new UnifiedResponse();

        if (page == null || limit == null) {
            page = 1;
            limit = 20;
        }

        log.info("batchGetComment parameter page : " + page + " limit : " + limit + " key : " + key);

        try {
            Resp resp = userFeign.commentList(page, limit, key);
            rpcDataUtil(response, resp);
        } catch (FeignException e) {
            response.setCode(ResponseInfo.SERVER_FAILED);
            response.setErrorMessage(ResponseInfo.RPC_ERROR);
            log.error("userFeign.del_users request error : " + e);
        }

        return response;
    }


    /**
     * 获取审核视频列表
     * @param page
     * @param limit
     * @param key
     * @param state
     * @return
     */
    @RequestMapping("/video_list")
    public Resp getVideoList(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam String key, @RequestParam Integer state) {
        if (page == null || limit == null) {
            page = 1;
            limit = 20;
        }

        if (state == null) {
            return Resp.error(ResponseInfo.PARAMETER_ERROR);
        }

        log.info("getVideoList parameter page : " + page + "limit : " + limit + " state : " + state + " key : " + key);

        try {
            return userFeign.getVideoList(page, limit, key, state);
        } catch (FeignException e) {
            log.error("userFeign.getVideoList request error : " + e);
        }

        return Resp.error(ResponseInfo.RPC_ERROR);

    }


    /**
     * 审核视频
     * @param hashMap
     * @return
     */
    @PostMapping("/video_verify")
    public Resp verifyVideo(@RequestBody Map<String, Object> hashMap) {

        if (hashMap == null) {
            log.error("verifyVideo parameter is null");
            return Resp.error(ResponseInfo.RPC_ERROR);
        }

        log.info("verifyVideo parameter hashMap : " + hashMap);

        try {
            return userFeign.verifyVideo(hashMap);
        }catch (FeignException e){
            log.error("userFeign.verifyVideo request error : " + e);
        }

        return Resp.error(ResponseInfo.RPC_ERROR);
    }

}
